package android.team.app.ui.missatoll;

import android.content.Intent;
import android.os.Bundle;
import android.team.app.R;
import android.team.app.ui.BaseActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class Vihicle_Info_Activity extends BaseActivity implements OnItemSelectedListener {

	private TextView tvTrailer;
	private LinearLayout lnFirst, lnSecond, lnThird, lnFourth;
	private EditText edPlate, edYear, edMake, edModel, edColor, edPlateTrailer,
			edYearTrailer, edMakeTrailer, edModelTrailer, edColorTrailer;
	private Spinner spinnerState, spinnerClass, spinnerStateTrailer;
	private ImageButton btNext;

	private String array_spinner[];
	private ArrayAdapter<String> adapter;
	private SpinnerAdapter adapter1;
	private int index;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.vehicle_info_activity);

		loadUI();
		loadSpecialUI();

		setTitleHeader(getString(R.string.vehicleinformation));

		addItemSpinner();		
		
	}

	private void loadSpecialUI() {

		tvTrailer = (TextView) findViewById(R.id.tvTrailer);
		lnFirst = (LinearLayout) findViewById(R.id.lnFirst);
		lnSecond = (LinearLayout) findViewById(R.id.lnSecond);
		lnThird = (LinearLayout) findViewById(R.id.lnThird);
		lnFourth = (LinearLayout) findViewById(R.id.lnFourth);
		btNext = (ImageButton) findViewById(R.id.btNext);
		btNext.setOnClickListener(this);
		
		spinnerState = (Spinner) findViewById(R.id.spinerState);
		
		spinnerClass = (Spinner) findViewById(R.id.spinnerClass);		
		spinnerClass.setOnItemSelectedListener(this);
		spinnerClass.setPrompt(getString(R.string.selectaclass));
		
		spinnerStateTrailer = (Spinner) findViewById(R.id.spinnerStateTrailer);

	}

	// Xây dựng phương thức ẩn và hiển thị khi người dùng chọn lớp xe
	private void setVisible() {
		tvTrailer.setVisibility(View.VISIBLE);
		lnThird.setVisibility(View.VISIBLE);
		lnFourth.setVisibility(View.VISIBLE);
	}
	
	private void setInvisible(){
		tvTrailer.setVisibility(View.INVISIBLE);
		lnThird.setVisibility(View.INVISIBLE);
		lnFourth.setVisibility(View.INVISIBLE);
	}

	// Xây dựng phương thức add item cho spinner
	private void addItemSpinner() {	
		
		array_spinner = new String[3];
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, array_spinner);

		array_spinner[0] = getString(R.string.automobileormotocycleoranyaxledtruck);
		array_spinner[1] = getString(R.string.threetofouraxles);
		array_spinner[2] = getString(R.string.fiveormoreaxles);
		
		spinnerClass.setAdapter(adapter);				

	}

	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position,
			long id) {
		
		index = spinnerClass.getSelectedItemPosition();

		if(index == 0){
			setInvisible();
		}		
		if(index == 1 || index == 2){
			setVisible();
		}		

	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		
		switch (v.getId()) {
		case R.id.btNext:
			startActivity(new Intent(Vihicle_Info_Activity.this, Trip_Info_Activity.class));
			break;

		default:
			break;
		}
		
	}

}
